<template>
	<view>
		<view class="search-box">
			<uni-search-bar @input="input" :radius="100" cancelButton="none" placeholder="搜索">
			</uni-search-bar>
		</view>
		<!-- 搜索建议区域 -->
		<view class="sugg-list" v-if="searchResults.length !== 0">
			<view class="sugg-item" v-for="(item,i) in searchResults" :key="i" @click="gotoDetail(item.goods_id)">
				 <view class="goods-name">{{item.goods_name}}</view>
				    <uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			<!-- 标题区域 -->
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="20" @click="clear"></uni-icons>
			</view>
			<!-- 列表区域 -->
			<view class="history-list">
				<uni-tag class="history-tag" :text="item" v-for="(item,i) in histories" :key="i" :inverted="true" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null, //控制搜索框防抖的数据,
				kw: '', //搜索关键词,
				searchResults: [] ,//搜索的结果列表
				historyList:['a','app','apple'] //搜索历史数据
			};
		},
		onLoad(){
			 this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
		},
		methods: {
			// input的输入事件
			input(e) {
				// console.log(e);
				clearTimeout(this.timer)
				// let that = this
				// 每次触发时间都会清除,之前的时间
				this.timer = setTimeout(() => {
				  this.kw = e
				  // 根据关键词，查询搜索建议列表
				 this.getSearchList()
				}, 500)
			},
			async	getSearchList(){
				if(this.kw.length === 0) {
					// 如果什么也没输入,就赋予空数组
					this.searchResults=[]
					return
				}
				const {data:res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query:this.kw})
				if(res.meta.status !==200) return uni.$showMsg()
				this.searchResults = res.message	
						this.saveSearchHistory()
				},
				gotoDetail(goods_id) {
					console.log('eee');
					uni.navigateTo({
						url:'/subpkg/goods_detail/goods_detail?goods_id=' + goods_id
					})
				},
				saveSearchHistory() {
					// 将搜索的词,存到历史搜索列表中
					// this.historyList.push(this.kw)
					const set = new Set(this.historyList)
					// 先删除再添加是为了每次将最新搜索放在第一位
					set.delete(this.kw)
					set.add(this.kw)
					// 将set转化为数组
					this.historyList = Array.from(set)
					 uni.setStorageSync('kw', JSON.stringify(this.historyList))
				},
				// 清空搜索历史
				clear(){
					this.historyList = []
					uni.setStorageSync('kw','[]')
				},
				gotoGoodsList(item) {
					uni.navigateTo({
						// 每个item就是kw的搜索关键字
						url:'/subpkg/goods_list/goods_list?query=' + item
					})
				}
			},
			computed:{
				histories() {
					return [...this.historyList].reverse()
				}
			}
			
		}
</script>

<style lang="scss">
	.search-box {
		position: sticky;
		top: 0;
		z-index: 999;
	}
	.sugg-list {
	padding: 0 5px;
		.sugg-item {
			font-size: 12px;
			padding: 13px 0 ;
			border-bottom: 1px solid #efefef;
			display: flex;
			align-items: center;
			justify-content: space-between;
			 .goods-name {
			      // 文字不允许换行（单行文本）
			      white-space: nowrap;
			      // 溢出部分隐藏
			      overflow: hidden;
			      // 文本溢出后，使用 ... 代替
			      text-overflow: ellipsis;
			      margin-right: 3px;
			    }
		}
		
	}
	.history-box {
		margin-top: 5px;
		.history-title {
			display: flex;
			justify-content: space-between;
			padding: 0 10px;
			height: 40px;
			line-height: 40px;
			border-bottom:1px solid #efefef ;
		}
		.history-list {
			margin: 10px 10px;
			// padding: 0 10px;
			.history-tag {
				margin-right: 5px;
			}
		}
	}
</style>
